﻿//
// ParallelGrep
//
// Copyright (C) 2011 - Tatsuya Niioka
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using ParallelGrep.Model;
using ParallelGrep.Properties;

namespace ParallelGrep.IO
{
    /// <summary>
    /// 進行状況ライタクラス
    /// </summary>
    public sealed class ProgressWriter
    {
        #region WriteCsv
        /// <summary>
        /// 指定した進行状況、テキストライタへCSV形式で書き出します。
        /// </summary>
        /// <param name="progresses">進行状況</param>
        /// <param name="writer">テキストライタ</param>
        public static void WriteCsv(IEnumerable<ProgressInfo> progresses, TextWriter writer)
        {
            string delimiter = ",";
            // header
            writer.Write(Resources.ProgressFileName);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressPath);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressSize);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressLineNumber);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressColumnIndex);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressText);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressEncoding);
            writer.WriteLine();
            // info
            foreach (var info in progresses)
            {
                writer.Write(info.FileName);
                writer.Write(delimiter);
                writer.Write(info.Path);
                writer.Write(delimiter);
                writer.Write(info.Size);
                writer.Write(delimiter);
                writer.Write(info.LineNumber);
                writer.Write(delimiter);
                writer.Write(info.ColumnIndex);
                writer.Write(delimiter);
                writer.Write(info.Text);
                writer.Write(delimiter);
                writer.Write(info.Encoding);
                writer.WriteLine();
            }
        }
        #endregion

        #region WriteTab
        /// <summary>
        /// 指定した進行状況、テキストライタへタブ形式で書き出します。
        /// </summary>
        /// <param name="progresses">進行状況</param>
        /// <param name="writer">テキストライタ</param>
        internal static void WriteTab(IEnumerable<ProgressInfo> progresses, TextWriter writer)
        {
            string delimiter = "\t";
            // header
            writer.Write(Resources.ProgressFileName);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressPath);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressSize);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressLineNumber);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressColumnIndex);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressText);
            writer.Write(delimiter);
            writer.Write(Resources.ProgressEncoding);
            writer.WriteLine();
            // info
            foreach (var info in progresses)
            {
                writer.Write(info.FileName);
                writer.Write(delimiter);
                writer.Write(info.Path);
                writer.Write(delimiter);
                writer.Write(info.Size);
                writer.Write(delimiter);
                writer.Write(info.LineNumber);
                writer.Write(delimiter);
                writer.Write(info.ColumnIndex);
                writer.Write(delimiter);
                writer.Write(info.Text);
                writer.Write(delimiter);
                writer.Write(info.Encoding);
                writer.WriteLine();
            }
        }
        #endregion

        #region WriteHtml
        /// <summary>
        /// 指定した進行状況、テキストライタへHTML形式で書き出します。
        /// </summary>
        /// <param name="progresses">進行状況</param>
        /// <param name="writer">テキストライタ</param>
        public static void WriteHtml(IEnumerable<ProgressInfo> progresses, TextWriter writer)
        {
            // header
            writer.WriteLine("<html>");
            writer.WriteLine("<body>");
            writer.WriteLine("<table>");
            writer.WriteLine("\t<tr>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressFileName));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressPath));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressSize));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressLineNumber));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressColumnIndex));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressText));
            writer.WriteLine("</td>");
            writer.Write("\t\t<td>");
            writer.Write(HttpUtility.HtmlEncode(Resources.ProgressEncoding));
            writer.WriteLine("</td>");
            writer.WriteLine("\t</tr>");
            writer.WriteLine();
            // info
            foreach (var info in progresses)
            {
                writer.WriteLine("\t<tr>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.FileName));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.Path));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.Size));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.LineNumber));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.ColumnIndex));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.Text));
                writer.WriteLine("</td>");
                writer.Write("\t\t<td>");
                writer.Write(HttpUtility.HtmlEncode(info.Encoding));
                writer.Write("</td>");
                writer.WriteLine("</tr>");
            }
            writer.Write("</table>");
            writer.WriteLine("</body>");
            writer.WriteLine("</html>");
        }
        #endregion
    }
}
